package UserProfile;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NewUserFrame extends JFrame{
	
	UserData userData = new UserData();
	JTextField userName = new JTextField("Kullanici Adi : ");
	JTextField enterUserName = new JTextField();
	JTextField password = new JTextField("Sifre : ");
	JTextField enterPassword = new JTextField();
	JTextField repeatPassword = new JTextField("Sifre (tekrar) : ");
	JTextField enterRepeatPassword = new JTextField();
	JTextField hiddenQuestion = new JTextField("Gizli Sorunuz : ");
	JTextField enterHiddenQuestion = new JTextField();
	JTextField answer = new JTextField("Cevabiniz : ");
	JTextField enterAnswer = new JTextField();
	JPanel emptyPanel = new JPanel();
	JButton okButton = new JButton("TAMAM");
	
	public NewUserFrame() {
		super("Yeni Kullanıcı Girişi");
		setLocationRelativeTo(null);
		setSize(300, 300);
		setResizable(false);
		setLayout(new GridLayout(6, 2));
		userName.setEditable(false);
		password.setEditable(false);
		repeatPassword.setEditable(false);
		hiddenQuestion.setEditable(false);
		answer.setEditable(false);
		
		add(userName);
		add(enterUserName);
		add(password);
		add(enterPassword);
		add(repeatPassword);
		add(enterRepeatPassword);
		add(hiddenQuestion);
		add(enterHiddenQuestion);
		add(answer);
		add(enterAnswer);
		add(emptyPanel);
		add(okButton);
		
		okButton.addActionListener(new okButtonListener());
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public class okButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(userData.contains(enterUserName.getText())) {
				JOptionPane.showMessageDialog(null, "Bu kullanici adi zaten var");
			}
			else if(!enterPassword.getText().equals(enterRepeatPassword.getText())) {
				JOptionPane.showMessageDialog(null, "Tekrar girilen sifre hatali");
			}
			else if(enterPassword.getText().length() < 6 || enterPassword.getText().length() > 8) {
				JOptionPane.showMessageDialog(null, "Sifre 6 ile 8 karakter arasi uzunlukta olmali!");
			}
			else if(enterUserName.getText().length() < 1 || enterHiddenQuestion.getText().length() < 1 ||
					enterAnswer.getText().length() < 1) {
				JOptionPane.showMessageDialog(null, "Doldurmadiginiz alanlar mevcut!");
				
			}
			else {
				userData.addUser(enterUserName.getText(), enterPassword.getText(), enterHiddenQuestion.getText(), 
						enterAnswer.getText());
				JOptionPane.showMessageDialog(null, "Kullanici basarili bir sekilde olusturuldu!");
			}
			
		}
		
	}
}
